package com.hxzy.shop.controller.commodity;

import com.github.pagehelper.PageInfo;
import com.hxzy.shop.common.dto.commodity.resp.ClassifiedManagementResp;
import com.hxzy.shop.dto.commodity.req.DrugInfoReq;
import com.hxzy.shop.dto.commodity.req.DrugPageReq;
import com.hxzy.shop.dto.commodity.req.DrugUpdateReq;
import com.hxzy.shop.dto.commodity.req.PageRequst;
import com.hxzy.shop.dto.commodity.resp.DrugResp;
import com.hxzy.shop.dto.marketing.dto.PageRequest;
import com.hxzy.shop.entity.MerchantPlugs;
import com.hxzy.shop.entity.Prescriptiondrug;
import com.hxzy.shop.feign.commodity.CommodityFeign;
import com.hxzy.shop.vo.ApiException;
import com.hxzy.shop.vo.Result;
import com.hxzy.shop.vo.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "处方药管理")
@RequestMapping("/prescription")
public class PrescriptiondrugController {

    @Resource
    private CommodityFeign commodityFeign;

    @ApiOperation("检测是否开通处方药售卖类目")
    @PostMapping("/checkOpenPre")
    public Result<Boolean> checkOpenPre() {
        Result<Boolean> booleanResult = commodityFeign.checkOpenPre();
        if (booleanResult.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, booleanResult.getMsg());
        }
        return Result.success(booleanResult.getData());
    }

    @ApiOperation("查询处方单/短信剩余数量")
    @PostMapping("/getPreInfo")
    public Result<MerchantPlugs> getPreInfo(@RequestBody PageRequst pageRequst){
        Result<MerchantPlugs> perInfo=commodityFeign.getPreInfo(pageRequst);
        if(perInfo.getCode() != ResultCode.SUCCESS.getCode()){
            throw  new ApiException(500,perInfo.getMsg());
        }
        return Result.success(perInfo.getData());
    }

    @ApiOperation("新建查询处方药列表(drugIdList为处方药的prescriptionId数组)")
    @PostMapping("/getPrescriptionList")
    public Result<PageInfo<Prescriptiondrug>> getPrescriptionList(@ApiParam(required = true, name = "drugPageReq", value = "处方药分页请求model")
                                                  @RequestBody DrugPageReq drugPageReq) {
        Result<PageInfo<Prescriptiondrug>> prescriptionList = commodityFeign.getPrescriptionList(drugPageReq);
        System.out.println("请求到了---Headquarters--getPrescriptionList");
        if (prescriptionList.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, prescriptionList.getMsg());
        }
        return Result.success(prescriptionList.getData());
    }

    //getClassifiedList
    /**
     * 获得分类列表
     * @return 分类列表
     */
    @ApiOperation("获得分类列表")
    @PostMapping("/getClassifiedList")
    public Result<List<ClassifiedManagementResp>> getClassifiedList() {
        Result<List<ClassifiedManagementResp>> classifiedList = commodityFeign.getClassifiedList();
        if (classifiedList.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, classifiedList.getMsg());
        }
        return Result.success(classifiedList.getData());
    }

    @ApiOperation("分页查询处方药品")
    @PostMapping("/getDrugPage")
    public Result<PageInfo<DrugResp>> getDrugPage(@ApiParam(required = true, name = "drugPageReq", value = "药品分页请求model")
                                              @RequestBody DrugPageReq drugPageReq) {
        Result<PageInfo<DrugResp>> drugPage = commodityFeign.getPreDrugPage(drugPageReq);
        if (drugPage.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, drugPage.getMsg());
        }
        return Result.success(drugPage.getData());
    }

    @ApiOperation("修改处方药状态")
    @PostMapping("/editState")
    public Result<Boolean> editState(@ApiParam(required = true, name = "addDrugReq", value = "修改处方药状态对象")
                                              @RequestBody DrugUpdateReq state) {
        Result<Boolean> booleanResult = commodityFeign.editState(state);
        if (booleanResult.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, booleanResult.getMsg());
        }
        return Result.success(booleanResult.getData());
    }


    @ApiOperation("查看处方药详情")
    @PostMapping("/showPrescription")
    public Result<DrugResp> showPrescription(@ApiParam(required = true, name = "addDrugReq", value = "商品的id集合")
                                           @RequestBody DrugInfoReq drugInfoReq) {
        Result<DrugResp> drugRespResult = commodityFeign.showPrescription(drugInfoReq);
        if (drugRespResult.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, drugRespResult.getMsg());
        }
        return Result.success(drugRespResult.getData());
    }


    @ApiOperation("删除处方药")
    @PostMapping("/deletePrescription")
    public Result<Boolean> deletePrescription(@ApiParam(required = true, name = "addDrugReq", value = "删除处方药对象")
                                      @RequestBody DrugInfoReq drugInfoReq) {
        Result<Boolean> booleanResult = commodityFeign.deletePrescription(drugInfoReq);
        if (booleanResult.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, booleanResult.getMsg());
        }
        return Result.success(booleanResult.getData());
    }

}
